﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1
{
    internal class Program
    {
        //享元模式(FlyWeight)：当系统存在大量的对象，并且这些对象又具有相同的内部状态时，我们就可以用享元模式共享相同的元件对象，以避免对象泛滥造成资源浪费。
        //享元思想：将对象可共享的部分和不可共享的部分分开，将可共享的部分作为元件，供多处使用
        static void Main(string[] args)
        {

        }
    }
   
    public interface IDrawable
    {
        void Draw(int x, int y);
    }

    #region 享元对象
    public class River : IDrawable
    {
        public River()
        {
            riverbitmap = new Bitmap(640,480);
        }
        private Bitmap riverbitmap;

        public void Draw(int x, int y)
        {
            Console.WriteLine("绘制河流");
        }
    }
    public class Grass : IDrawable
    {
        public Grass()
        {
            Grassbitmap = new Bitmap(640, 480);
        }
        private Bitmap Grassbitmap;

        public void Draw(int x, int y)
        {
            Console.WriteLine("绘制草坪");
        }
    }
    public class Stone : IDrawable
    {
        public Stone()
        {
            Stonebitmap = new Bitmap(640, 480);
        }
        private Bitmap Stonebitmap;

        public void Draw(int x, int y)
        {
            Console.WriteLine("绘制石头路");
        }
    }
    public class House : IDrawable
    {
        private Bitmap riverbitmap;
        public void Draw(int x, int y)
        {
            Console.WriteLine("绘制房屋");
        }
    }
    #endregion

    #region 享元对象工厂
    public class TileFactory
    {
        private Dictionary<string,IDrawable> Images;
        public TileFactory()
        {
            Images = new Dictionary<string,IDrawable>();
        }

        public IDrawable GetImage(string image)
        {
            if (this.Images.ContainsKey(image))
            {
                return this.Images[image];
            }
            else
            {
                switch (image)
                {
                    case "河流":
                        this.Images.Add(image, new River());
                        return this.Images[image];
                    case "房屋":
                        this.Images.Add(image, new House());
                        return this.Images[image];
                    case "石头路":
                        this.Images.Add(image, new Stone());
                        return this.Images[image];
                    case "草坪":
                        this.Images.Add(image, new Grass());
                        return this.Images[image];
                    default:
                        this.Images.Add(image, new Grass());
                        return this.Images[image];
                }
            }
        }
    }
    #endregion 
}
